<?php

namespace App\Http\Requests\System;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class SysUserRequest extends FormRequest
{
    /**
     * 表示验证器是否应在第一个规则失败时停止
     *
     * @var bool
     */
    protected $stopOnFirstFailure = true;

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     */
    public function rules(): array
    {
        return [
            'username' => ['required', Rule::unique('App\Models\SysUser')->ignore($this->route('id')), 'max:64'],
            'email' => 'nullable|email',
            'mobile' => 'nullable|digits:11',
            'gender' => 'required',
            'password' => 'nullable|max:128',
        ];
    }

    /**
     * 获取验证错误的自定义属性
     *
     * @return string[]
     */
    public function attributes()
    {
        return [
            'username' => '用户名称',
            'email' => '邮箱',
            'mobile' => '手机号码',
            'gender' => '性别',
            'password' => '密码',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息
     *
     * @return string[]
     */
    public function messages()
    {
        return [
            'username.required' => '用户名称不能为空',
            'username.unique' => '用户名称已存在',
            'gender.required' => '性别不能为空',
            'email.email' => '邮箱格式不正确',
            'mobile.digits' => '手机号码格式错误',
        ];
    }
}
